package com.example.javachatroomextend.controller;

import com.example.javachatroomextend.constant.Constant;
import com.example.javachatroomextend.model.Message;
import com.example.javachatroomextend.model.Result;
import com.example.javachatroomextend.model.User;
import com.example.javachatroomextend.service.MessageService;
import com.example.javachatroomextend.service.TodoMessageService;
import com.example.javachatroomextend.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.datatype.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @Autowired
    private TodoMessageService todoMessageService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @PostMapping("/getMessageInfos")
    public Result<List<Message>> getMessageInfos(Integer sessionId, HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user1 = (User) session.getAttribute("user");
        List<String> result = stringRedisTemplate.opsForList().range(Constant.key +sessionId,0,-1);
        List<Message> ret = new ArrayList<>();
        if (result != null && !result.isEmpty()) {
            for (String s : result) {
                try {
                    Message message = objectMapper.readValue(s, Message.class);
                    ret.add(message);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
            return Result.success(ret);
        }
        ret = messageService.getMessageBySessionId(sessionId);
        User user = null;
        for (Message message : ret) {
            user = userService.getUserByUserId(message.getFromId());
            message.setImage(user.getImage());
            try {
                stringRedisTemplate.opsForList().rightPush(Constant.key+sessionId,objectMapper.writeValueAsString(message));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        stringRedisTemplate.expire(Constant.key+sessionId,1,TimeUnit.HOURS );
        Collections.reverse(ret);
        todoMessageService.deleteTodoCount(user1.getUserId(),sessionId);
        return Result.success(ret);
    }
}
